function init()
{
	$.getJSON('/GoGoGadget/AuthenticationServlet', initCallback);
}

function initCallback(data)
{
	var isLoggedIn = data.value;
	if(isLoggedIn)
	{
		$.getJSON('/GoGoGadget/businessServlet', displayinfo);
	}
	else {
		alert("Please login");
		document.location = "/GoGoGadget/StartPage.jsp";
	}
}

function displayinfo(data)
{
	
		var name = data.business;
		var tel = data.telephone;
		var cell = data.cell;
		var city = data.city;
		var email = data.email;
		var fax = data.fax;
		var post = data.postal;
		var prov = data.province;
		var address = data.address;
		
		document.title = name;
		if (name != undefined && name != "undefined"){
			document.getElementById("name").appendChild(document.createTextNode(name));
		}
		if (email != undefined && email != "undefined"){
			document.getElementById("email").appendChild(document.createTextNode(email));
		}
		if (tel != undefined && tel != "undefined"){
			document.getElementById("tel").appendChild(document.createTextNode(tel));
		}
		if (cell != undefined && cell != "undefined"){
			document.getElementById("cell").appendChild(document.createTextNode(cell));
		}
		if (city != undefined && city != "undefined"){
			document.getElementById("city").appendChild(document.createTextNode(city));
		}
		if (fax != undefined && fax != "undefined"){
			document.getElementById("fax").appendChild(document.createTextNode(fax));
		}
		if (post != undefined && post != "undefined"){
			document.getElementById("post").appendChild(document.createTextNode(post));
		}
		if (prov != undefined && prov != "undefined"){
			document.getElementById("prov").appendChild(document.createTextNode(prov));
		}
		if (address != undefined && address != "undefined"){
			document.getElementById("address").appendChild(document.createTextNode(address));
		}
		
}

//function getClients()
//{
	//$.getJSON('/GoGoGadget/listClientsServlet', displayClients);
//}
function getClients()
{
	$.getJSON('/GoGoGadget/AuthenticationServlet', clientCallback);
}

function clientCallback(data)
{
	var isLoggedIn = data.value;
	if(isLoggedIn)
	{
		$.getJSON('/GoGoGadget/listClientsServlet', displayClients);
	}
	else {
		alert("Please login");
		document.location = "/GoGoGadget/StartPage.jsp";
	}
}

function displayClients(data)
{
	var index;
	var clients = data.clients;
	var list = document.getElementById("list");
	
	
	for (index=0; index<clients.length; index++) 
	{
		
		var listElement = document.createElement("li");
		
		var client = clients[index];
		
		listElement.appendChild(document.createTextNode(client));
		
		listElement.setAttribute("client", client);
		listElement.onclick = clientProfile;
		listElement.onmouseover = highlight;
		listElement.onmouseout = unhighlight;
		
		list.appendChild(listElement);
		
	}
	
}

function clientProfile()
{
	var client = this.getAttribute("client");
	//change the next part
	//alert(client);
	$.getJSON('/GoGoGadget/GetClientProfileServlet?' + 'client=' + client, getClientProfile);
}

function getClientProfile(data)
{
	/*if(data.reset == true)
	{
		reset();
	}*/
	document.location = "ClientInfo.jsp?" +
						"first=" + data.first +
						"&last=" + data.last +
						"&cell=" + data.cell +
						"&city=" + data.city +
						"&email=" + data.email +
						"&tel1=" + data.tel1 +
						"&tel2=" + data.tel2 +
						"&postal=" + data.postal +
						"&province=" + data.province +
						"&address=" + data.street +
						"&clientID="+ data.clientID;
						
}

function setupClientInfo()
{
	var url = new ArgumentURL();
	var arg;

	arg = url.getArgument("first");
	
	if (arg != undefined && arg != "undefined"){
		arg = arg + " " + url.getArgument("last");
		if (arg != undefined && arg != "undefined"){
			document.getElementById("name").appendChild(document.createTextNode(arg));
		}
		document.title = arg;
	}
	
	arg = url.getArgument("cell");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("cell").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("city");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("city").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("email");
	if (arg != undefined && arg != "undefined"){
		var c = document.getElementById("email");
		c.setAttribute("clientEmail", arg);
		c.appendChild(document.createTextNode(arg));
		c.onclick = getSender;
		c.onmouseover = highlight;
		c.onmouseout = unhighlight;
	}
	
	arg = url.getArgument("tel1");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("tel1").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("tel2");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("tel2").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("postal");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("post").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("province");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("prov").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("address");
	if (arg != undefined && arg != "undefined"){
		var a = document.getElementById("address");
		a.setAttribute("address", arg);
		a.appendChild(document.createTextNode(arg));
		a.onclick = mapClient;
		a.onmouseover = highlight;
		a.onmouseout = unhighlight;
	}
	
	arg = url.getArgument("clientID");
	if (arg != undefined && arg != "undefined"){
		var p = document.getElementById("clientID");
		p.appendChild(document.createTextNode(arg));
		p.setAttribute("clientID", arg);
		
	}
}


function highlight()
{
	this.style.backgroundColor = '#dcfac9';
}

function unhighlight()
{
	this.style.backgroundColor = 'white';
}

function ArgumentURL() {
	this.getArgument = _getArg;
	this.setArgument = _setArg;
	this.removeArgument = _removeArg;
	this.toString    = _toString;	//Allows the object to be printed
					//no need to write toString()
	this.arguments   = new Array();

	// Initiation
	var separator = "&";
	var equalsign = "=";
	
	var str = window.location.search.replace(/%20/g, " ");
	var index = str.indexOf("?");
	var sInfo;
	var infoArray = new Array();

	var tmp;
	
	if (index != -1) {
		sInfo = str.substring(index+1,str.length);
		infoArray = sInfo.split(separator);
	}

	for (var i=0; i<infoArray.length; i++) {
		tmp = infoArray[i].split(equalsign);
		if (tmp[0] != "") {
			var t = tmp[0];
			this.arguments[tmp[0]] = new Object();
			this.arguments[tmp[0]].value = tmp[1];
			this.arguments[tmp[0]].name = tmp[0];
		}
	}
	

	
	function _toString() {
		var s = "";
		var once = true;
		for (i in this.arguments) {
			if (once) {
				s += "?";
				once = false;
			}
			s += this.arguments[i].name;
			s += equalsign;
			s += this.arguments[i].value;
			s += separator;
		}
		return s.replace(/ /g, "%20");
	}
	
	function _getArg(name) {
		if(this.arguments[name] == undefined) {
			return null;
		}
		
		if (typeof(this.arguments[name].name) != "string") {
			return null;
		}
		else {
			return this.arguments[name].value;
		}
	}
	
	function _setArg(name,value) {
		this.arguments[name] = new Object();
		this.arguments[name].name = name;
		this.arguments[name].value = value;
	}
	
	function _removeArg(name) {
		this.arguments[name] = null;
	}
	
	return this;
}

function getSender()
{
	var sender = this.getAttribute("clientEmail");
	document.location = "EmailPage.jsp?" +
	"sender="+ sender;
}

function mapClient()
{	
	
	var cAddress = document.getElementById("address").getAttribute("address");
	$.getJSON('/GoGoGadget/ClientToGoogleMapServlet?cAddress=' + cAddress, mappingClient);
}

function mappingClient(data)
{
	document.location = "GoogleMapsPage.jsp?" +
	"businessAddress=" + data.address +
	"&clientAddress=" + data.cAddress ;
}

function editThisClient()
{
	
	var ID = document.getElementById("clientID").getAttribute("clientID");
	$.getJSON('/GoGoGadget/ClientEditServlet?clientID=' + ID, sendToEditClient);
}

function sendToEditClient(data)
{
	document.location = "EditClient.jsp?" +
	"first=" + data.first +
	"&last=" + data.last +
	"&tel1=" + data.tel1 +
	"&tel2=" + data.tel2 +
	"&cell=" + data.cell +
	"&city=" + data.city +
	"&email=" + data.email +
	"&postal=" + data.postal +
	"&province=" + data.province +
	"&address=" + data.street +
	"&clientID="+ data.clientID;
	
}

function clientInfoload()
{
	var urlParser = new ArgumentURL();
	var element;
	
	var text = urlParser.getArgument("first");
	if(text == null){text = "";}
	if(text!=undefined) {
		element = document.getElementById("first");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	var text = urlParser.getArgument("last");
	if(text == null){text = "";}
	if(text!=undefined) {
		element = document.getElementById("last");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = urlParser.getArgument("email");
	if(text == null){text = "";}
	if(text!=undefined) {
		element = document.getElementById("email");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = urlParser.getArgument("tel1");
	if(text == null){text = "";}
	if(text!=undefined) {
		element = document.getElementById("tel1");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = urlParser.getArgument("tel2");
	if(text == null){text = "";}
	if(text!=undefined) {
		element = document.getElementById("tel2");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = urlParser.getArgument("cell");
	if(text == null){text = "";}
	if(text!=undefined) {
		element = document.getElementById("cell");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = urlParser.getArgument("address");
	if(text == null){text = "";}
	if(text!=undefined) {
		element = document.getElementById("address");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = urlParser.getArgument("prov");
	if(text == null){text = "";}
	if(text!=null) {
		element = document.getElementById("prov");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = urlParser.getArgument("city");
	if(text == null){text = "";}
	if(text!=undefined) {
		element = document.getElementById("city");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = urlParser.getArgument("post");
	if(text == null){text = "";}
	if(text!=undefined) {
		element = document.getElementById("post");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = urlParser.getArgument("clientID");
	if(text == null){text = "";}
	if(text!=undefined) {
		element = document.getElementById("clientID");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
}

function clientInfoEdit()
{
	document.getElementById("first").disabled = false;
	document.getElementById("last").disabled = false;
	document.getElementById("email").disabled = false;
	document.getElementById("tel1").disabled = false;
	document.getElementById("tel2").disabled = false;
	document.getElementById("cell").disabled = false;
	document.getElementById("address").disabled = false;
	document.getElementById("prov").disabled = false;
	document.getElementById("city").disabled = false;
	document.getElementById("post").disabled = false;
	
	var buttons = document.getElementById("buttons");
	while(buttons.hasChildNodes())
		buttons.removeChild(buttons.firstChild);
	
	var button;
	
	button = document.createElement("button");
	button.appendChild(document.createTextNode("Submit Changes"));
	button.onclick = submitClientEdit;
	buttons.appendChild(button);
	
	button = document.createElement("button");
	button.appendChild(document.createTextNode("Revert Changes"));
	button.onclick = revertClientEdit;
	buttons.appendChild(button);
}

function deleteClient()
{
	var urlParser = new ArgumentURL();
	
	var clientID = urlParser.getArgument("clientID");
	$.getJSON('/GoGoGadget/DeleteClientServlet?clientID=' + clientID, deleteCallBack);
	
}

function deleteCallBack(data)
{
	document.location = "/GoGoGadget/ClientList.jsp";
}

function revertClientEdit()
{
	var element;
	
	element = document.getElementById("first");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("last");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("email");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("tel1");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("tel2");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("cell");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("address");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("prov");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("city");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("post");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	var buttons = document.getElementById("buttons");
	while(buttons.hasChildNodes())
		buttons.removeChild(buttons.firstChild);
	
	var button = document.createElement("button");
	button.appendChild(document.createTextNode("Edit"));
	button.onclick = clientInfoEdit;
	buttons.appendChild(button);
	
	var button = document.createElement("button");
	button.appendChild(document.createTextNode("Delete Client"));
	button.onclick = deleteClient;
	buttons.appendChild(button);
}

function submitClientEdit()
{
	var url = '/GoGoGadget/ClientChangesServlet';
	
	var isfirst=true;
	var element;
	
	element = document.getElementById("clientID");
	if (element.value != "") 
	{
		isfirst=false;
		url += '?clientID=' + element.value;
	}
	
	element = document.getElementById("first");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'first=' + element.value;
	}
	
	element = document.getElementById("last");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'last=' + element.value;
	}
	
	element = document.getElementById("cell");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'cell=' + element.value;
	}
	
	element = document.getElementById("city");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'city=' + element.value;
	}
	
	element = document.getElementById("email");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'email=' + element.value;
	}
	
	element = document.getElementById("tel1");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'tel1=' + element.value;
	}
	
	element = document.getElementById("tel2");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'tel2=' + element.value;
	}
	
	element = document.getElementById("post");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'post=' + element.value;
	}
	
	element = document.getElementById("prov");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'prov=' + element.value;
	}
	
	
	element = document.getElementById("address");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'address=' + element.value;
	}
	
	$.getJSON(url, getClientProfile);
}
